package com.baby.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baby.mall.product.entity.ProductAttrValueEntity;
import com.baby.mall.product.entity.vo.AttrGroupRelationVo;
import com.baby.mall.product.entity.vo.AttrRespVo;
import com.baby.mall.product.entity.vo.AttrVo;
import com.baby.mall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baby.mall.product.entity.AttrEntity;
import com.baby.mall.product.service.AttrService;
import com.baby.common.utils.PageUtils;
import com.baby.common.utils.R;



/**
 * 商品属性
 *
 * @author qinglongzhai
 * @email qinglongzhai@gmail.com
 * @date 2021-03-15 10:47:09
 */
@Api(tags = "商品属性")
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;


    @ApiOperation("查询规格参数")
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable Long catelogId,
                          @PathVariable String attrType){
        PageUtils page= attrService.queryBaseAttrPage(params,catelogId,attrType);

        return R.ok().put("page", page);

    }


    @ApiOperation("修改规格")
    @GetMapping("/base/listforspu/{spuId}")
    public  R baseAttrListforspu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> entities= productAttrValueService.baseAttrListforspu(spuId);
        return R.ok().put("data",entities);

    }

    /**
     * 列表
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("信息")
    @GetMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo= attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }


    /**
     * 规格维护，确认修改
     */
    @ApiOperation("规格维护，确认修改")
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();

    }



    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
